<template>
  <div class="app-container">
    <el-form
      v-show="showSearch"
      ref="queryForm"
      :model="queryParams"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="审核类型" prop="status">
        <el-select v-model="queryParams.type" placeholder="请选择" clearable>
          <el-option :key="'0'" label="评论" :value="'0'"/>
          <el-option :key="'1'" label="留言" :value="'1'"/>
        </el-select>
      </el-form-item>
      <el-form-item label="审核状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择" clearable>
          <el-option :key="'0'" label="审核通过" :value="'0'"/>
          <el-option :key="'1'" label="审核未通过" :value="'1'"/>
          <el-option :key="'2'" label="未审核" :value="'2'"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >搜索
        </el-button>
        <!--        <el-button-->
        <!--          icon="el-icon-refresh"-->
        <!--          size="mini"-->
        <!--          @click="resetQuery"-->
        <!--        >重置</el-button>-->
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
        >导出
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-circle-check"
          size="mini"
          :disabled="multiple"
          @click="handleChangeStatus(null,'0')"
        >通过审核
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-circle-close"
          size="mini"
          :disabled="multiple"
          @click="handleChangeStatus(null,'1')"
        >不通过审核
        </el-button>
      </el-col>
    </el-row>

    <el-table
      v-loading="loading"
      :data="commentList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="id" align="center" prop="id"/>
      <el-table-column label="类型" align="center" prop="type">
        <template slot-scope="scope">
          {{ scope.row.type == '0' ? "评论" : "留言" }}
        </template>
      </el-table-column>
      <!--      <el-table-column label="所属文章" align="center" prop="articleId" />-->
      <el-table-column label="内容" align="center" prop="content"/>
      <el-table-column prop="status" label="审核状态" width="80px" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status == 0" type="success">通过</el-tag>
          <el-tag v-if="scope.row.status == 1" type="danger">未通过</el-tag>
          <el-tag v-if="scope.row.status == 2" type="danger">未审核</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime"/>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
          <el-button
            style="color: #6ce26c"
            v-if="scope.row.status == 2"
            size="mini"
            type="text"
            icon="el-icon-circle-check"
            @click="handleChangeStatus(scope.row, '0')"
          >通过审核
          </el-button>
          <el-button
            style="color: red"
            v-if="scope.row.status == 2"
            size="mini"
            type="text"
            icon="el-icon-circle-close"
            @click="handleChangeStatus(scope.row, '1')"
          >不通过审核
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      :page-size.sync="queryParams.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      :page-sizes="[10, 20, 30, 40]"
      :current-page.sync="queryParams.pageNum"
      @current-change="getList"
      @size-change="getList"
    />

  </div>
</template>

<script>
import {changeCommentStatus, delComment, exportComment, listComment} from '@/api/content/comment'

export default {
  name: 'Comment',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      statuList:[],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 评论表格数据
      commentList: null,
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        articleId: null,
        type: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询友链列表 */
    getList() {
      this.loading = true
      listComment(this.queryParams).then((response) => {
        this.commentList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    handleChangeStatus(comment, newStatus) {
      let ids = null
      let statusList = null
      if (null == comment) {
        ids = this.ids
        statusList = this.statuList
      } else {
        ids = comment.id
        statusList = comment.status
      }
      this.loading = true
      changeCommentStatus(ids, statusList, newStatus).then((response) => {
        this.loading = false
        this.$modal.msgSuccess('审核成功')
        this.open = false
        this.getList()
      }).catch(()=>{
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    /** 导出按钮操作 */
    handleExport() {
      this.$modal.confirm('是否确认导出所有评论数据？').then(() => {
        this.exportLoading = true
        return exportComment()
      }).then(response => {
        // this.$download.name(response.msg)
        this.exportLoading = false
      }).catch(() => {
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id)
      this.statuList = selection.map((item) => item.status)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal
        .confirm('是否确认删除评论编号为"' + ids + '"的数据项？')
        .then(function () {
          return delComment(ids)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {
        })
    }
  }
}
</script>
